﻿using DataTransfer.Core.Domains;
using DataTransfer.Core.Providers;
using DataTransfer.SDK.Models;
using Platform.Data.Entities.MySql;
using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Utility;
using DataTransfer.Core.Services;

namespace DataTransfer.Data
{
    [AutoResolve(ExcelExportEngine.COMPONENT_NAME)]
    public class ExcelExporterProvider : EntityDataProvider<PlatformDbContext>, IExportConfigProvider
    {
        public IExporter GetExporter(string name)
        {
            var e = this.CurrentDbContext.DtExcelExport.SingleOrDefault(e => e.Name == name);
            if (e == null) return null;
            var exporter = new ExcelExporter
            {
                Name = e.Name,
                //Template = e.Template,
                DataLoaderTypeName = e.DataLoader,
                ExcelFormat = Enum.Parse<ExcelFormat>(e.ExcelFormat),
                MaxRowCountPerSheet = e.MaxRowCountPerSheet,
                Sheets = e.Sheets.ToJsonObject<ExcelExportSheet[]>()
            };

            exporter.Modules = new Dictionary<string, Type>(e.Modules.ToJsonObject<Dictionary<string, string>>().Select(p =>
            {
                Type type = Type.GetType(p.Value);
                return new KeyValuePair<string, Type>(p.Key, type);
            }));

            return exporter;
        }
    }
}
